package com.android.fragmentdemo.defineviews
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.ViewGroup
import androidx.core.view.get
import com.android.fragmentdemo.R
import com.android.fragmentdemo.databinding.ListDataRvSubItemBinding
import com.android.fragmentdemo.findBinding
import com.android.fragmentdemo.showToast

class ExpandItemViewGroup @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : ViewGroup(context, attributeSet) {

    private var mTotalWidth = 0
    private var mTotalHeight = 0

    private val mListDataRvSubItemBinding: ListDataRvSubItemBinding

    private var mItemData: String? = null

    init {
        setBackgroundColor(context.resources.getColor(R.color.color_0BABF3))
        mListDataRvSubItemBinding = findBinding(R.layout.list_data_rv_sub_item)
        mListDataRvSubItemBinding.subItemRoot.setOnClickListener {
            mItemData?.let {
                context.showToast(it)
            }
        }
        addView(
            mListDataRvSubItemBinding.root,
            MarginLayoutParams(MarginLayoutParams.MATCH_PARENT, MarginLayoutParams.WRAP_CONTENT)
        )
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        for (i in 0.until(childCount)) {
            val child = get(i)
            measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0)
            Log.d(TAG, "$TAG---child.measuredHeight---${child.measuredHeight}")
        }

        mTotalWidth = when (MeasureSpec.getMode(widthMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(widthMeasureSpec)
            }

            else -> {
                context.resources.getDimension(R.dimen.dp_60).toInt()
            }
        }

        mTotalHeight = when (MeasureSpec.getMode(heightMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(heightMeasureSpec)
            }

            else -> {
                context.resources.getDimension(R.dimen.dp_45).toInt()
            }
        }

        setMeasuredDimension(
            MeasureSpec.makeMeasureSpec(mTotalWidth, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(mTotalHeight, MeasureSpec.EXACTLY)
        )
    }

    override fun onLayout(force: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        val child = get(0)
        val childWidth = child.measuredWidth
        val childHeight = child.measuredHeight
        child.layout(
            0,
            0,
            childWidth,
            childHeight
        )
    }

    override fun generateLayoutParams(attrs: AttributeSet?): LayoutParams {
        return MarginLayoutParams(context, attrs)
    }

    override fun performClick(): Boolean {
        return super.performClick()
    }

    fun setItemData(itemData: String?) {
        Log.d(TAG,"is null---${itemData == null}")
        mItemData = itemData
        mItemData?.let {
            mListDataRvSubItemBinding.tvBodySubItem.text = it
        }
    }

    companion object {
        const val TAG = "ExpandViewGroup"
    }
}